package org.xtext.runtime.plan;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.xtext.runtime.formula.MentalEvent;
import org.xtext.runtime.formula.Goal;
import org.xtext.runtime.interpreter.Agent;
import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;

public class SpawnGoalStep implements IPlanStep {
	Goal formula;
	
	public SpawnGoalStep(Goal formula) {
		this.formula = formula;
	}

	@Override
	public SpawnGoalStep apply(Bindings set) {
		formula.apply(set);
		return this;
	}

	@Override
	public void handle(Agent agent, PlanStack planStack) {
		if (Goal.class.isInstance(formula)) {
			agent.addEvent(new MentalEvent(MentalEvent.ADDITION, formula, null));
			agent.logAction("!" + formula + " [SUCCESS]");
		}
	}

	@Override
	public String toString(String tabs) {
		return tabs + "!" + formula;
	}

	public String toFullString(String tabs) {
		return toString(tabs);
	}
	
	@Override
	public Set<Variable> variables() {
		Set<Variable> variables = new HashSet<Variable>();
//		variables.addAll(this.formula.getVariables());
		return variables;
	}

	@Override
	public SpawnGoalStep copy(Map<String, Variable> vars) {
		return new SpawnGoalStep(formula.copy(vars));
	}

	@Override
	public void reset() {
	}
}
